package com.settlement.system.parse.lang;

import lombok.Data;
import lombok.ToString;

import java.util.Calendar;
import java.util.Date;

/**
 * 订单稽核日期
 *
 * @author Ximi
 * @since 2020/10/10
 */
@Data
@ToString
public class OrderDate implements Comparable<OrderDate> {

    private int year;

    /**
     * 月份从0-11
     */
    private int month;

    /**
     * 日期从0-29
     */
    private int day;

    public OrderDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public OrderDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH) - 1;
    }

    @Override
    public int compareTo(OrderDate o) {

        if (year != o.getYear()) {
            return year - o.getYear();
        }

        if (month != o.getMonth()) {
            return month - o.getMonth();
        }

        if (day != o.getDay()) {
            return day - o.getDay();
        }

        return 0;
    }

    public OrderDate addition(OrderDate orderDate) {
        this.year += orderDate.getYear();
        this.month += orderDate.getMonth();
        this.day += orderDate.getDay();
        format();
        return this;
    }

    public OrderDate substract(OrderDate orderDate) {
        this.year -= orderDate.getYear();
        this.month -= orderDate.getMonth();
        this.day -= orderDate.getDay();
        format();
        return this;
    }


    private void format() {

        int cmonth = this.day / 30;
        this.day = this.day % 30;
        if (this.day < 0) {
            cmonth--;
            this.day += 30;
        }

        this.month += cmonth;
        int cyear = this.month / 12;
        this.month = this.month % 12;
        if (this.month < 0) {
            cyear--;
            this.month += 12;
        }

        this.year += cyear;
    }

}
